package util;

import java.util.Date;

public class IdUniqueHelper {
	private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";// "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	private static final long BASE = 36;
	private static final String DIGIT = "0123456789";

	public static String encode(long num) {
		StringBuilder sb = new StringBuilder();

		while (num > 0) {
			sb.append(ALPHABET.charAt((int) (num % BASE)));
			num /= BASE;
		}

		return sb.reverse().toString();
	}

	public static String encodeNum(long num) {
		StringBuilder sb = new StringBuilder();

		while (num > 0) {
			sb.append(DIGIT.charAt((int) (num % 9)));
			num /= 9;
		}

		return sb.reverse().toString();
	}

	public static long decode(String str) {
		long num = 0;

		for (int i = 0, len = str.length(); i < len; i++) {
			num = num * BASE + ALPHABET.indexOf(str.charAt(i));
		}

		return num;
	}

	public static String getId() {
		Date date = new Date();
		String id = encode(date.getTime());
		return id;
	}

}
